package com.sx.pipeline.ui.bank

import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.BankListAdapter
import com.sx.pipeline.adapter.MessageListAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActBankListBinding
import com.sx.pipeline.repos.http.res.BankListVO
import com.sx.pipeline.repos.http.res.MessageListVo


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 银行卡列表
 */
@Route(path = RoutePath.Wallet.BANK_CARD)
class BankListActivity :
    CYDataBindingBaseActivity<ActBankListBinding, BankVM>(R.layout.act_bank_list),
    View.OnClickListener {
    private lateinit var mAdapter: BankListAdapter

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

    }

    override fun onResume() {
        super.onResume()
        mViewModel.getBankCardList()
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.getBankCardList(mPage)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.getBankCardList(mPage)
            }
        })
    }

    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 银行卡列表
        setBankList()

    }

    private fun setBankList() {
        // 消息列表
        observe(mViewModel.bankListEvent) {
            if (it != null && it.records.isNotEmpty()) {
                mBinding.recyclerView.visibility=View.VISIBLE
//                mBinding.emptyView.visibility = View.GONE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        this, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter = BankListAdapter(it.records as MutableList<BankListVO>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->

                    }
                    mAdapter.deleteBankCallback = {id:Int->
                        XPopup.Builder(this)
                            .isDestroyOnDismiss(true)
                            .asConfirm(
                                getString(R.string.hint), getString(R.string.txt_unbind_bankcard),
                                getString(R.string.cancel), getString(R.string.confirm),
                                {
                                    //解绑银行卡
                                    mViewModel.deleteBank(id)
                                }, null, false
                            ).show()
                    }
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<BankListVO>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
//                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility = View.GONE
            }
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            //添加银行卡
            R.id.cv_add -> ARouter.getInstance().build(RoutePath.Wallet.BANK_CARD_ADD).navigation()
        }
    }

}